//
//  LLT_ItemInfoViewController_LXM.m
//  CoderLLT
//
//  Created by 刘晓萌 on 15/12/11.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_ItemInfoViewController_LXM.h"
#import "UIImageView+WebCache.h"
#import "LLT_HeroInfoViewController_LXM.h"

@interface LLT_ItemInfoViewController_LXM ()<UIWebViewDelegate,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *itemName;

@property (weak, nonatomic) IBOutlet UIWebView *heroInfoWebView;
@property (weak, nonatomic) IBOutlet UIWebView *itemInfoWebView;

@property (assign, nonatomic) NSInteger indexScrollFlag;
@property (weak, nonatomic) IBOutlet UIImageView *flagImageView;
@property (weak, nonatomic) IBOutlet UILabel *FirstTitleLable;
@property (weak, nonatomic) IBOutlet UILabel *secondTitleLable;
@property (weak, nonatomic) IBOutlet UIScrollView *playerScroll;

@end

@implementation LLT_ItemInfoViewController_LXM

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUrlsForWebView];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickLeftTitleLabelAction)];
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickRightTitleLabelAction)];
    [self.FirstTitleLable addGestureRecognizer:leftTap];
    [self.secondTitleLable addGestureRecognizer:rightTap];
    
    self.FirstTitleLable.userInteractionEnabled = YES;
    self.secondTitleLable.userInteractionEnabled = YES;
    self.playerScroll.delegate = self;
}
#pragma mark --- scrollView代理方法和 滑动点击
/*
 * scrollView滑动调用方法
 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.indexScrollFlag = self.playerScroll.contentOffset.x/self.view.frame.size.width;

    if (self.indexScrollFlag) {
        self.secondTitleLable.textColor = [UIColor whiteColor];
        self.FirstTitleLable.textColor = [UIColor grayColor];
        CGFloat x = self.secondTitleLable.center.x - (self.flagImageView.frame.size.width/2);
        [UIView animateWithDuration:0.3 animations:^{
            self.flagImageView.frame = CGRectMake(x, self.flagImageView.frame.origin.y,self.flagImageView.frame.size.width,self.flagImageView.frame.size.height);
        }];
        
    }else{
        self.secondTitleLable.textColor = [UIColor grayColor];
        self.FirstTitleLable.textColor = [UIColor whiteColor];
        CGFloat x = self.FirstTitleLable.center.x - (self.flagImageView.frame.size.width/2);
        [UIView animateWithDuration:0.3 animations:^{
            self.flagImageView.frame = CGRectMake(x, self.flagImageView.frame.origin.y,self.flagImageView.frame.size.width,self.flagImageView.frame.size.height);
        }];
    }
    
}

/**
 * 滑动点击方法
 */
-(void)didClickLeftTitleLabelAction{
    self.playerScroll.contentOffset = CGPointMake(0, self.playerScroll.contentOffset.y);
}
-(void)didClickRightTitleLabelAction{
    self.playerScroll.contentOffset = CGPointMake(self.view.frame.size.width, self.playerScroll.contentOffset.y);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 * 数据加载和webView赋值
 */
-(void)setUrlsForWebView{
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.itemUrls.pic]];
    self.itemName.text = self.itemUrls.title;
    
    self.heroInfoWebView.delegate = self;
    self.itemInfoWebView.delegate = self;
    
    for (NSDictionary *dic in self.itemUrls.pages) {
        NSMutableString *rootUrl = self.itemUrls.rootUrl.mutableCopy;
        if ([dic[@"name"] isEqualToString:@"使用英雄"]) {
            [rootUrl appendString:dic[@"path"]];
            [self.heroInfoWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:rootUrl]]];
            continue;
        }
        if ([dic[@"name"] isEqualToString:@"物品详情"]) {
            [rootUrl appendString:dic[@"path"]];
            [self.itemInfoWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:rootUrl]]];
            continue;
        }
    }
}
/**
 * uiwebView按状态响应加载
 *
 * @return
 */
#pragma mark --- uiwebView 协议
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *str = [request URL].absoluteString;
    str = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSString *strsub = [str substringToIndex:6];
    NSString *s = @"maxjia://PageConfigJson#/";
    if ([strsub isEqualToString:@"maxjia"]) {
        str =  [str stringByReplacingOccurrencesOfString:s withString:@""];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData: [str dataUsingEncoding:NSUTF8StringEncoding]
                                                            options: NSJSONReadingMutableContainers
                                                              error: nil];
        
        LLT_DetailOfArticleModel *itemUrls = [[LLT_DetailOfArticleModel alloc]init];
        [itemUrls setValuesForKeysWithDictionary:dic[@"result"]];
        BOOL flag = YES;
        for (NSDictionary *dict in itemUrls.pages) {
            if ([dict[@"name"] isEqualToString:@"物品详情"]) {
                flag = NO;
            }
        }
        if (flag) {
            LLT_HeroInfoViewController_LXM *heroInfoVC = [MainStoryBoard instantiateViewControllerWithIdentifier:@"heroInfoVC"];
            heroInfoVC.heroUrls = itemUrls;
            [self presentViewController:heroInfoVC animated:YES completion:nil];
        }else{
            LLT_ItemInfoViewController_LXM *itemInfoVC = [MainStoryBoard instantiateViewControllerWithIdentifier:@"itemInfoVC"];
            itemInfoVC.itemUrls = itemUrls;
            [self presentViewController:itemInfoVC animated:YES completion:nil];
        }
    }

    
    return YES;
}


/**
 * 进行url
 */
- (IBAction)didClickLeftButton:(UIButton *)sender {
    DissMissBack;
}


/**
 * 设置电源栏反颜色
 *
 * @return
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
